package com.example.project.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.project.entity.Attachment;

/**
 * 附件，存储系统中所有上传的文件信息Mapper接口
 * 
 * @author ruoyi
 * @date 2025-09-23
 */
@Mapper
public interface AttachmentMapper {

	/**
	 * 查询附件，存储系统中所有上传的文件信息列表
	 * 
	 * @param tAttachments 附件，存储系统中所有上传的文件信息
	 * @return 附件，存储系统中所有上传的文件信息集合
	 */
	@Select({ "<script>",
			"select id, container_id, container_type, filename, disk_filename, file_path, filesize, content_type, digest, downloads, author_id, "
			+ "del_flag, create_by, create_time, update_by, update_time from t_attachment where del_flag = 0",
			"  <if test='containerId != null'> and container_id = #{containerId}</if>",
			"  <if test='containerType != null and containerType != \"\"'> and container_type = #{containerType}</if>",
			"  <if test='filename != null and filename != \"\"'> and filename like concat('%', #{filename}, '%')</if>",
			"  <if test='diskFilename != null and diskFilename != \"\"'> and disk_filename like concat('%', #{diskFilename}, '%')</if>",
			"  <if test='filesize != null'> and filesize = #{filesize}</if>",
			"  <if test='contentType != null and contentType != \"\"'> and content_type = #{contentType}</if>",
			"  <if test='digest != null and digest != \"\"'> and digest = #{digest}</if>",
			"  <if test='downloads != null'> and downloads = #{downloads}</if>",
			"  <if test='authorId != null'> and author_id = #{authorId}</if>", "</script>" })
	List<Attachment> selectAttachmentList(Attachment tAttachments);

	/**
	 * 查询附件，存储系统中所有上传的文件信息
	 * 
	 * @param id 附件，存储系统中所有上传的文件信息主键
	 * @return 附件，存储系统中所有上传的文件信息
	 */
	@Select("select id, container_id, container_type, filename, disk_filename, file_path, filesize, content_type, digest, downloads, author_id, "
			+ "del_flag, create_by, create_time, update_by, update_time from t_attachment where del_flag = 0 and id = #{id}")
	Attachment selectAttachmentById(Long id);

	/**
	 * 新增附件，存储系统中所有上传的文件信息
	 * 
	 * @param tAttachments 附件，存储系统中所有上传的文件信息
	 * @return 结果
	 */
	@Insert({ "<script>", "insert into t_attachment", "<trim prefix='(' suffix=')' suffixOverrides=','>",
			"  <if test='containerId != null'>container_id,</if>",
			"  <if test='containerType != null and containerType != \"\"'>container_type,</if>",
			"  <if test='filename != null and filename != \"\"'>filename,</if>",
			"  <if test='diskFilename != null and diskFilename != \"\"'>disk_filename,</if>",
			"  <if test='filePath != null'>file_path,</if>",
			"  <if test='filesize != null'>filesize,</if>",
			"  <if test='contentType != null and contentType != \"\"'>content_type,</if>",
			"  <if test='digest != null and digest != \"\"'>digest,</if>",
			"  <if test='downloads != null'>downloads,</if>", "  <if test='authorId != null'>author_id,</if>",
			"<if test=\"delFlag != null\">del_flag,</if>",  
			"<if test=\"createBy != null\">create_by,</if>", "<if test=\"createTime != null\">create_time,</if>",
			"<if test=\"updateBy != null\">update_by,</if>", "<if test=\"updateTime != null\">update_time,</if>", "</trim>",
			"<trim prefix='values (' suffix=')' suffixOverrides=','>",
			"  <if test='containerId != null'>#{containerId},</if>",
			"  <if test='containerType != null and containerType != \"\"'>#{containerType},</if>",
			"  <if test='filename != null and filename != \"\"'>#{filename},</if>",
			"  <if test='diskFilename != null and diskFilename != \"\"'>#{diskFilename},</if>",
			"  <if test='filePath != null'>#{filePath},</if>",
			"  <if test='filesize != null'>#{filesize},</if>",
			"  <if test='contentType != null and contentType != \"\"'>#{contentType},</if>",
			"  <if test='digest != null and digest != \"\"'>#{digest},</if>",
			"  <if test='downloads != null'>#{downloads},</if>", "  <if test='authorId != null'>#{authorId},</if>",
			"<if test=\"delFlag != null\">#{delFlag},</if>", 
			"<if test=\"createBy != null\">#{createBy},</if>", "<if test=\"createTime != null\">#{createTime},</if>",
			"<if test=\"updateBy != null\">#{updateBy},</if>", "<if test=\"updateTime != null\">#{updateTime},</if>", "</trim>", "</script>" })
	@Options(useGeneratedKeys = true, keyProperty = "id")
	long insertAttachment(Attachment tAttachments);

	/**
	 * 修改附件，存储系统中所有上传的文件信息
	 * 
	 * @param tAttachments 附件，存储系统中所有上传的文件信息
	 * @return 结果
	 */
	@Update({ "<script>", "update t_attachment", "<trim prefix='SET' suffixOverrides=','>",
			"  <if test='containerId != null'>container_id = #{containerId},</if>",
			"  <if test='containerType != null and containerType != \"\"'>container_type = #{containerType},</if>",
			"  <if test='filename != null and filename != \"\"'>filename = #{filename},</if>",
			"  <if test='diskFilename != null and diskFilename != \"\"'>disk_filename = #{diskFilename},</if>",
			"  <if test='filePath != null'>file_path = #{filePath},</if>",
			"  <if test='filesize != null'>filesize = #{filesize},</if>",
			"  <if test='contentType != null and contentType != \"\"'>content_type = #{contentType},</if>",
			"  <if test='digest != null and digest != \"\"'>digest = #{digest},</if>",
			"  <if test='downloads != null'>downloads = #{downloads},</if>",
			"  <if test='authorId != null'>author_id = #{authorId},</if>",
			"<if test=\"updateBy != null\">update_by = #{updateBy},</if>",
			"<if test=\"updateTime != null\">update_time = #{updateTime},</if>", "</trim>", "where id = #{id}",
			"</script>" })
	int updateAttachment(Attachment tAttachments);

	/**
	 * 删除附件，存储系统中所有上传的文件信息
	 * 
	 * @param id 附件，存储系统中所有上传的文件信息主键
	 * @return 结果
	 */
//	@Delete("delete from t_attachment where id = #{id}")
	@Update("update t_attachment set del_flag = 1 where id = #{id}")
	int deleteAttachmentById(Long id);

	/**
	 * 批量删除附件，存储系统中所有上传的文件信息
	 * 
	 * @param ids 需要删除的数据主键集合
	 * @return 结果
	 */
//	@Delete({ "<script>", "delete from t_attachment where id in",
//			"<foreach item='id' collection='array' open='(' separator=',' close=')'>", "  #{id}", "</foreach>",
//			"</script>" })
	@Update("<script>" + "update t_attachment set del_flag = 1 where id in "
			+ "<foreach item='id' collection='array' open='(' separator=',' close=')'>" + "#{id}" + "</foreach>"
			+ "</script>")
	int deleteAttachmentByIds(@Param("array") String[] ids);

	@Select("select id, container_id, container_type, filename, disk_filename, file_path, filesize, content_type, digest, downloads, author_id, "
			+ "del_flag, create_by, create_time, update_by, update_time from t_attachment "
			+ "where del_flag = 0 and container_id = #{containerId} and container_type = #{containerType}")
	List<Attachment> queryAttachmentsListByIdAndType(Long containerId, String containerType);

	/**
	 * 根据ID列表查询附件列表
	 * 
	 * @param ids 附件ID列表
	 * @return 附件列表
	 */
	@Select({ "<script>",
			"select id, container_id, container_type, filename, disk_filename, file_path, filesize, content_type, digest, downloads, author_id, "
			+ "del_flag, create_by, create_time, update_by, update_time from t_attachment where del_flag = 0 ",
			"  <if test='list != null and list.size() > 0'>",
			"   and id in",
			"    <foreach item='id' collection='list' open='(' separator=',' close=')'>", "      #{id}", "</foreach>",
			"  </if>",
			"</script>" })
	List<Attachment> selectAttachmentListByIds(@Param("list") List<Long> ids);

	@Select("select id, container_id, container_type, filename, disk_filename, file_path, filesize, content_type, digest, downloads, author_id, "
			+ "del_flag, create_by, create_time, update_by, update_time from t_attachment where del_flag = 0 and container_type = #{containerType}")
	int countByContainer(String containerType);

	/**
	 * 根据容器类型查询附件列表，按创建时间升序排序
	 * 
	 * @param containerType 容器类型
	 * @return 附件列表
	 */
	@Select("select id, container_id, container_type, filename, disk_filename, file_path, filesize, content_type, digest, downloads, author_id, "
			+ "del_flag, create_by, create_time, update_by, update_time from t_attachment where del_flag = 0 and container_type = #{containerType} "
			+ "order by create_time asc")
	List<Attachment> findByContainerOrderByCreatedAtAsc(String containerType);

	@Select("select container_type from t_attachment where del_flag = 0 and container_type = #{containerType} ")
	String findContainerById(Long objectId);
}
